// 自定义枚举
export class CustomEnum {
  constructor (enums, configs) {
    this.enums = enums
    this.configs = configs ?? { valueKey: 'value', labelKey: 'label' }
    const { options, items, maps, values, labels } = this.initEnums()
    this.options = options
    this.items = items
    this.maps = maps
    this.values = values
    this.labels = labels
  }
  /**
   * 初始化枚举各属性
   * options - { value, label } 组成的options List集合
   * items - value-label组成的item Object结果集
   * maps - value映射label的maps Object结果集
   * values - value组成的values Object结果集
   * labels - value组成的labels Object结果集
   */
  initEnums () {
    let options = [], items = {}, maps = {}, values = {}, labels = {}
    for (const key in this.enums) {
      const [value, label] = this.enums[key]
      items[key] = {
        [this.configs.valueKey]: value,
        [this.configs.labelKey]: label
      }
      options.push(items[key])
      maps[value] = label
      values[key] = value
      labels[key] = label
    }
    return { options, items, maps, values, labels }
  }
  // 获取过滤不需要的枚举对象的options List集合
  omitOptions (keys) {
    if (!keys || keys.length == 0) return this.options
    const vals = keys.map(key => this.values[key])
    return this.options.filter(item => !vals.includes(item.value))
  }
  // 获取需要的枚举对象的options List集合
  pickOptions (keys) {
    if (!keys || keys.length == 0) return this.options
    const vals = keys.map(key => this.values[key])
    return this.options.filter(item => vals.includes(item.value))
  }
}

// 表格信息类
export class TableInfo {
  constructor ({ columns, list, loading, selection, dimensionKey } = {}) {
    this.columns = columns ?? [] // 表列
    this.list = list ?? [] // 表格数据
    this.loading = loading ?? false // 表格加载loading
    this.selection = selection ?? [] // 表格勾选数据
    this.dimensionKey = dimensionKey ?? 'uuid' // 表格唯一标识属性key
    this.setLoading = this.setLoading.bind(this)
    this.setSelection = this.setSelection.bind(this)
    this.getListAttrs = this.getListAttrs.bind(this)
    this.getSelectionAttrs = this.getSelectionAttrs.bind(this)
  }
  // 获取表列集合
  getColumns () {
    return _.cloneDeep(this.columns)
  }
  // 设置表列集合
  setColumns (columns) {
    this.columns = _.cloneDeep(columns)
  }
  // 清空表列集合
  clearColumns () {
    this.columns = []
  }
  // 获取表格数据
  getList () {
    return _.cloneDeep(this.list)
  }
  // 设置表格数据
  setList (list = []) {
    this.list = _.cloneDeep(list)
  }
  // 清空表格数据
  clearList () {
    this.list = []
  }
  // 设置表格加载loading
  setLoading (state = false) {
    this.loading = state
  }
  // 获取表格勾选集合
  getSelection () {
    return _.cloneDeep(this.selection)
  }
  // 设置表格勾选集合
  setSelection (selection = []) {
    this.selection = selection
  }
  // 清空表格勾选集合
  clearSelection () {
    this.selection = []
  }
  // 表格集合为空
  isNullList () {
    return this.list.length == 0
  }
  // 表格勾选集合为空
  isNullSelection () {
    return this.selection.length == 0
  }
  // 表格集合删除某项
  removeList (target, idKey = this.dimensionKey) {
    _.isObject(target) ? this.removeByAttr(this.list, target, idKey) : this.removeByIndex(this.list, target)
  }
  // 按索引下标删除表格对应项
  removeByIndex (arrays, index) {
    arrays.splice(index, 1)
  }
  // 按唯一标识属性值删除表格对应项
  removeByAttr (arrays, row, attrKey) {
    const index = arrays.findIndex(item => item[attrKey] == row[attrKey])
    index > -1 && arrays.splice(index, 1)
  }
  // 获取某个属性值集合
  getAttrs (arrays, prop = this.dimensionKey) {
    return arrays.map(item => item[prop]).filter(item => item)
  }
  // 获取表格集合某个属性值集合
  getListAttrs (prop) {
    return this.getAttrs(this.list, prop)
  }
  // 获取表格勾选数据集合某个属性值集合
  getSelectionAttrs (prop) {
    return this.getAttrs(this.selection, prop)
  }
}
